/**
 *
 * 2007-11-1 下午07:46:32
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.normal.SaturnNormalDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnSwichNode extends Shape {

	private static final long serialVersionUID = 2946279234083480438L;

	private Image image = Activator.getImage(Images.SWICH_MAX);
	public static final String SWICH_NODE_NAME = "switch";
	private static int i = 1;
	private List<SaturnSwichVO> saturnSwichConnections = new ArrayList<SaturnSwichVO>();

	public SaturnSwichNode() {
		super.setName(Messages.getString("SaturnSwichNode.swtich") + (i++)); //$NON-NLS-1$
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node swichNode = SaturnW3CUtil.createChildNode(root, SWICH_NODE_NAME,
				null);

		SaturnW3CUtil.createChildNode(swichNode, KEY_PARAMETER_ATTRIBUTE_NAME,
				super.getName());
		
		for (SaturnSwichVO saturn : this.saturnSwichConnections) {
			swichNode.appendChild(saturn.creatNode(swichNode));
		}

		return swichNode;
	}

	public Dialog getDialog() {
		SaturnNormalDialog dialog = new SaturnNormalDialog(super.getShell(), this);
		return dialog;
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		return null;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	public List<SaturnSwichVO> getSaturnSwichConnections() {
		return saturnSwichConnections;
	}

	public void setSaturnSwichConnections(
			List<SaturnSwichVO> saturnSwichConnections) {
		if (this.saturnSwichConnections.size() != saturnSwichConnections.size()) {
			this.saturnSwichConnections = saturnSwichConnections;
			firePropertyChange(PROPERTERY_CHANGS, null, saturnSwichConnections);
			return;
		}
		for (SaturnSwichVO saturn : this.saturnSwichConnections) {
			for (SaturnSwichVO newSaturn : saturnSwichConnections) {
				if (!saturn.equals(newSaturn)) {
					firePropertyChange(PROPERTERY_CHANGS, null,
							saturnSwichConnections);
					return;
				}
			}
		}
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("description", getDescription());
		
		para.setInfourl( name + "-router.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.SwtichNode")); //$NON-NLS-1$
		para.setImagename("router");
		String templatePath = "template/export/logic/switch";
		String Path = filePath + File.separator + name + "-router.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,infos, Path);
		
		return para;
	}
}
